const MobileDetect = require('mobile-detect');

module.exports = function (req, res, next) {
    const userAgent = req.headers['user-agent'] || '';
    const md = new MobileDetect(userAgent);

    // 三重检测保障
    req.isMobile = (
        !!md.mobile() ||                     // mobile-detect库的检测
        !!md.tablet() ||                     // 包含平板设备
        /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent) // 正则检测
    );

    // 更详细的设备分类（可选）
    req.deviceType = md.mobile() ? 'mobile' :
        md.tablet() ? 'tablet' :
            'desktop';

    // 调试信息
    // console.log('User-Agent:', userAgent);
    // console.log('Mobile detect:', md.mobile());
    // console.log('isMobile:', req.isMobile);
    // console.log('Device type:', req.deviceType);

    next();
};